<template>
  <el-card shadow="never">
    <template #header>
      <div class="card-header">
        <el-form :inline="true" :model="params.entity"
                 style="padding-bottom: 0px;">
          <el-form-item label="用户名称">
            <el-input v-model="params.entity.adminId"
                      placeholder="请输入用户名称"/>
          </el-form-item>
          <el-form-item label="商户名称">
            <el-input v-model="params.entity.shopId"
                      placeholder="请输入商户名称"/>
          </el-form-item>
          <el-form-item label="审核状态">
            <!--            <el-input v-model="params.entity.examineVerify"-->
            <!--                      placeholder="请输入审核状态 0-待审核 1-审核通过 2-审核不通过"/>-->

            <el-select v-model="params.entity.examineVerify" clearable
                       placeholder="请选择审核状态"  style="width:160px">
              <el-option :label="'待审核'" :value=0></el-option>
              <el-option :label="'审核通过'" :value=1></el-option>
              <el-option :label="'审核不通过'" :value=2></el-option>
            </el-select>
          </el-form-item>
          <!--          <el-form-item label="状态 0-停用 1-启用 2-备用">-->
          <!--            <el-input v-model="params.entity.statusFlag"-->
          <!--                      placeholder="请输入状态 0-停用 1-启用 2-备用"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="是否删除 0-否 1-是">-->
          <!--            <el-input v-model="params.entity.delFlag"-->
          <!--                      placeholder="请输入是否删除 0-否 1-是"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="创建人">-->
          <!--            <el-input v-model="params.entity.createUser"-->
          <!--                      placeholder="请输入创建人"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="创建时间">-->
          <!--            <el-input v-model="params.entity.createTime"-->
          <!--                      placeholder="请输入创建时间"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="修改时间">-->
          <!--            <el-input v-model="params.entity.updateTime"-->
          <!--                      placeholder="请输入修改时间"/>-->
          <!--          </el-form-item>-->
          <!--          <el-form-item label="备注">-->
          <!--            <el-input v-model="params.entity.remark"-->
          <!--                      placeholder="请输入备注"/>-->
          <!--          </el-form-item>-->
          <el-form-item>
            <el-button type="primary" :icon="Search" @click="searchData">查询
            </el-button>
            <!--            <el-button type="primary" :icon="Plus" @click="addDialog">新增</el-button>-->
          </el-form-item>
        </el-form>
      </div>
    </template>
    <el-table v-loading="loading"
              :data="tableData.list" style="width: 100%" height="500"
              :default-sort="{ prop: 'date', order: 'descending' }"
              @selection-change="handleSelectionChange">
      <!--      <el-table-column fixed type="selection" width="55"/>-->
      <el-table-column prop="id" label="ID" show-overflow-tooltip/>
      <el-table-column prop="adminName" label="用户名称" show-overflow-tooltip/>
      <el-table-column prop="shopName" label="商户名称" show-overflow-tooltip/>
      <el-table-column prop="examineVerifyName" label="审核状态" show-overflow-tooltip>
        <template #default="scope">
          <el-tag v-if="scope.row.examineVerify == 0" class="ml-2" type="danger">{{scope.row.examineVerifyName}}</el-tag>
          <el-tag v-if="scope.row.examineVerify == 1" class="ml-2" type="success">{{scope.row.examineVerifyName}}</el-tag>
          <el-tag v-if="scope.row.examineVerify == 2" class="ml-2" type="info">{{scope.row.examineVerifyName}}</el-tag>
        </template>
      </el-table-column>
      <!--      <el-table-column prop="statusFlag" label="状态 0-停用 1-启用 2-备用"-->
      <!--                       show-overflow-tooltip/>-->
      <!--      <el-table-column prop="delFlag" label="是否删除 0-否 1-是"-->
      <!--                       show-overflow-tooltip/>-->
      <!--      <el-table-column prop="createUser" label="创建人"-->
      <!--                       show-overflow-tooltip/>-->
      <!--      <el-table-column prop="createTime" label="创建时间"-->
      <!--                       show-overflow-tooltip/>-->
      <el-table-column prop="updateTime" label="审核日期" show-overflow-tooltip/>
      <el-table-column prop="remark" label="审核信息" show-overflow-tooltip/>
      <el-table-column fixed="right" label="操作" width="100"
                       show-overflow-tooltip>
        <!--        <template #header>-->
        <!--          <el-input v-model="params.search" @keyup.enter.native="searchData" placeholder="搜索角色名称/介绍"/>-->
        <!--        </template>-->
        <template #default="scope">
          <!--          <el-button link type="danger" :icon="Delete" size="small"-->
          <!--                     @click="showDeleteDialog(scope.row.id)">删除-->
          <!--          </el-button>-->
          <!--          <el-button link type="primary" :icon="Edit" size="small"-->
          <!--                     @click="editDialog(scope.row)">编辑-->
          <!--          </el-button>-->

          <el-button v-if="scope.row.examineVerify == 0" link type="primary" :icon="Edit" size="small"
                     @click="examineVerifyOpen(scope.row)">审核
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination v-model:currentPage="params.current"
                   v-model:page-size="params.size"
                   :page-sizes="[100, 200, 300, 400]"
                   layout="sizes, prev, pager, next" :total="tableData.total"
                   @size-change="handleSizeChange"
                   @current-change="handleCurrentChange"/>
  </el-card>

  <!-- 新增或者修改页面-->
  <RetailAdminShopAddOrEdit
      @closeEditDialog="closeEditDialog"
      :addOrEditVisible="addOrEditVisible"
      :ruleForm="ruleForm"
      :addDataType="addDataType"></RetailAdminShopAddOrEdit>


  <el-dialog
      v-model="examineVerifyVisible"
      title="审核"
      width="30%"
      align-center
  >
    <span>
       <el-input type="textarea" v-model="examineVerifyRemark"/>
    </span>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="examineVerifyNotPass">不通过</el-button>
        <el-button type="primary" @click="examineVerifyPass">通过</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import {ElTable, ElMessage, ElMessageBox} from 'element-plus'
import type {Action} from 'element-plus'
import {onMounted, computed, reactive, ref} from 'vue'
import {Params, Result} from "@/apis/model/common/common.interfaces"
import {
  Delete,
  Edit,
  Search,
  Share,
  Upload,
  Plus,
  Setting
} from '@element-plus/icons-vue'
import RetailAdminShopAddOrEdit from './retailAdminShopAddOrEdit.vue'
import {RetailAdminShop}
  from "@/apis/model/shop/RetailAdminShopModel";
import {
  deleteRetailAdminShopById, examineVerify,
  retailAdminShopPage
} from "@/apis/http/shop/RetailAdminShopHttp";

// 查询
const params: Params = reactive({
  current: 1,
  size: 10,
  search: '',
  entity: {}
})

const ruleForm = ref<RetailAdminShop>({} as RetailAdminShop)

// AddOrEditVisible
const addOrEditVisible = ref(false)

// table 表数据
const tableData: any = ref({});

// 加载数据
const loading = ref(true)
const addDataType = ref('add');


const examineVerifyVisible = ref(false)
const currentExamineVerify = ref({} as RetailAdminShop)
const examineVerifyRemark = ref('')

/**
 * 初始化数据
 */
const initList = () => {
  loading.value = true
  retailAdminShopPage(params).then((res: Result) => {
    const code = res.code
    const message = res.message
    const data = res.data
    if (code == 200) {
      tableData.value = data
    }
    loading.value = false
  })
}

/**
 * search 查询数据
 */
const searchData = () => {
  initList()
}

/**
 * 每页数据量改变查询数据
 * @param val
 */
const handleSizeChange = (val: number) => {
  params.size = val
  initList()
}

/**
 * 分页查询数据
 * @param val
 */
const handleCurrentChange = (val: number) => {
  params.current = val
  initList()
}

/**
 * 添加弹出
 */
const addDialog = () => {
  addOrEditVisible.value = true
  ruleForm.value = {} as RetailAdminShop
  addDataType.value = 'add'
}

//修改弹窗
const editDialog = (row: RetailAdminShop) => {
  addOrEditVisible.value = true
  ruleForm.value = row
  addDataType.value = 'edit'
}

// 关闭修改弹窗
const closeEditDialog = (e: any) => {
  addOrEditVisible.value = e.addOrEditVisible
  if (e.refushList = true) {
    params.current = 1;
    initList();
  }
}

const examineVerifyData = (_id: number, examineVer: number, _remark: string) => {
  examineVerify({
    id: _id,
    examineVerify: examineVer,
    remark: _remark
  } as RetailAdminShop).then(res => {
    ElMessage({
      type: 'success',
      message: `审核成功！`,
    })
    initList();
  }).catch(res => {
    ElMessage({
      type: 'error',
      message: `审核失败！`,
    })
  })
}

/**
 * 审核信息
 * @param row
 */
const examineVerifyOpen = (row: RetailAdminShop) => {
  examineVerifyVisible.value = true;
  currentExamineVerify.value = row;
  examineVerifyRemark.value = '';
  // ElMessageBox.prompt('请输入审核信息', 'Tip', {
  //   confirmButtonText: '通过',
  //   cancelButtonText: '不通过',
  //   inputErrorMessage: '审核信息',
  // }).then(({value}) => {
  //   examineVerifyData(row.id, 1, value);
  // }).catch((action: Action, {value}) => {
  //   debugger;
  //   if (action === 'cancel') {
  //     examineVerifyData(row.id, 2, '111');
  //   }
  // })
}

const examineVerifyNotPass = () => {
  examineVerifyVisible.value = false;
  examineVerifyData(currentExamineVerify.value.id, 2, examineVerifyRemark.value)
}

const examineVerifyPass = () => {
  examineVerifyVisible.value = false;
  examineVerifyData(currentExamineVerify.value.id, 1, examineVerifyRemark.value)
}

/**
 * 弹出删除对话框
 */
const showDeleteDialog = (id: number) => {
  ElMessageBox.confirm(
      '是否删除?',
      '提醒',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
  ).then(() => {
    deleteRetailAdminShopById(id).then((res: Result) => {
      const code = res.code;
      if (code == 200) {
        ElMessage({
          message: '删除数据成功！',
          type: 'success',
        })
        initList();
      } else {
        ElMessage.error('删除数据失败！');
      }
    })
  }).catch(() => {
    // ElMessage({
    //   type: 'info',
    //   message: 'Delete canceled',
    // })
  })
}

onMounted(() => {
  initList()
})
</script>

<style>
</style>
